package com.itheima.mall.api.oms;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.convert.Convert;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.mall.dto.OrderDto;

import com.itheima.mall.mapper.oms.OrderItemMapper;
import com.itheima.mall.mapper.oms.OrderMapper;
import com.itheima.mall.mapper.oms.OrderOperateHistoryMapper;
import com.itheima.mall.pojo.oms.OmsOrder;
import com.itheima.mall.pojo.oms.OmsOrderItem;
import com.itheima.mall.pojo.oms.OmsOrderOperateHistory;

import com.itheima.mall.vo.OrderDetailVo;
import com.itheima.mall.vo.PageResult;
import com.itheima.mall.vo.R;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;

@DubboService
public class OrderApiImpl implements OrderApi{

    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private OrderOperateHistoryMapper orderOperateHistoryMapper;

    @Autowired
    private OrderItemMapper orderItemMapper;

    @Override
    public void deleteOrderByIds(Integer[] ids) {
        //不是真正删除，而是改状态
        for (Integer id : ids) {
            Long orderId = Convert.toLong(id);
            LambdaQueryWrapper<OmsOrder> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(OmsOrder::getId,orderId);
            orderMapper.delete(wrapper);
        }
    }
    @Override
    public PageResult listAll(OrderDto orderDto) {
       //return orderMapper.selectList(null);
        Page pageCondition = new Page(orderDto.getPageNum(),orderDto.getPageSize());
        Page pageR = orderMapper.selectPage(pageCondition, null);
        PageResult pageResult = new PageResult();
        pageResult.setList(pageR.getRecords());
        pageResult.setPageSize(orderDto.getPageSize());
        pageResult.setPageNum(orderDto.getPageNum());
        pageResult.setTotalPage(Convert.toInt(pageR.getPages()));
        pageResult.setTotal(Convert.toInt(pageR.getTotal()));
        return pageResult;
    }
    @Override
    public void updateNote(Integer id, String note, Integer status) {
        OmsOrder omsOrder = orderMapper.selectById(id);
        omsOrder.setNote(note);
        omsOrder.setStatus(status);
        return;
    }

    /**
     * 修改收货人信息
     * @param omsOrder
     */
    @Override
    public void updateReceiverInfo(OmsOrder omsOrder) {
        orderMapper.updateById(omsOrder);
    }


    /**
     * 批量关闭订单
     * @param ids
     * @param note
     */
    @Override
    public void closeOrder(Integer[] ids, String note) {
        for (Integer id : ids) {
            Long orderId = Convert.toLong(id);
            OmsOrder omsOrder = orderMapper.selectById(orderId);
            omsOrder.setNote(note);
            orderMapper.updateById(omsOrder);
        }
    }

    @Override
    public void deliveryOrder(HashMap deliveryParamList) {
        Integer orderId = (Integer) deliveryParamList.get("orderId");
        LambdaQueryWrapper<OmsOrder> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(OmsOrder::getId,orderId);
        OmsOrder omsOrder = orderMapper.selectOne(wrapper);
        String deliveryCompany = (String) deliveryParamList.get("deliveryCompany");
        String deliverySn = (String) deliveryParamList.get("deliverySn");
        omsOrder.setDeliveryCompany(deliveryCompany);
        omsOrder.setDeliverySn(deliverySn);
    }

    @Override
    public void updateMoneyInfo(HashMap moneyInfoParam) {
        BigDecimal discountAmount = (BigDecimal) moneyInfoParam.get("discountAmount");
        BigDecimal freightAmount = (BigDecimal) moneyInfoParam.get("freightAmount");
        Integer orderId = (Integer) moneyInfoParam.get("orderId");
        Integer status = (Integer) moneyInfoParam.get("status");
        LambdaQueryWrapper<OmsOrder> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(OmsOrder::getId,orderId);
        OmsOrder omsOrder = orderMapper.selectOne(wrapper);
        omsOrder.setDiscountAmount(discountAmount);
        omsOrder.setFreightAmount(freightAmount);
        omsOrder.setStatus(status);
    }

    @Override
    public OrderDetailVo orderDetail(Integer id) {
        OmsOrder omsOrder = orderMapper.selectById(id);
        OrderDetailVo orderDetailVo = new OrderDetailVo();
        BeanUtil.copyProperties(omsOrder,orderDetailVo);
        LambdaQueryWrapper<OmsOrderOperateHistory> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(OmsOrderOperateHistory::getOrderId,id);
        List<OmsOrderOperateHistory> omsOrderOperateHistoryList = orderOperateHistoryMapper.selectList(wrapper);
        orderDetailVo.setHistoryList(omsOrderOperateHistoryList);
        LambdaQueryWrapper<OmsOrderItem> wrapper1 = new LambdaQueryWrapper<>();
        wrapper1.eq(OmsOrderItem::getOrderId,id);
        List<OmsOrderItem> omsOrderItemList = orderItemMapper.selectList(wrapper1);
        orderDetailVo.setOrderItemList(omsOrderItemList);
        return orderDetailVo;
    }
}
